#!/usr/bin/php
<?php
set_time_limit(0);

require_once __DIR__ . DIRECTORY_SEPARATOR . "init.php";

/* 当前时间 */
$datetime = date('Y-m-d H:i:s');

/* 判断cdn_log_dir */
if (!isset($config->cdn_log_dir) || empty($config->cdn_log_dir)) {
    exit('[' . $datetime . ']' . ' ERROR: 缺少配置项cdn_log_dir或cdn_log_dir为空！' . PHP_EOL);
}

/* 判断log_start_time */
if (!isset($config->log_start_time) || empty($config->log_start_time)) {
    exit('[' . $datetime . ']' . ' ERROR: 缺少配置项log_start_time或log_start_time为空！' . PHP_EOL);
}

/* 判断log_end_time */
if (!isset($config->log_end_time) || empty($config->log_end_time)) {
    exit('[' . $datetime . ']' . ' ERROR: 缺少配置项log_end_time或log_end_time为空！' . PHP_EOL);
}

/* 判断cdn_host */
if (!isset($config->cdn_host) || empty($config->cdn_host)) {
    exit('[' . $datetime . ']' . ' ERROR: 缺少配置项cdn_host或cdn_host为空！' . PHP_EOL);
}

if (!initRemoteDBConn()) {
    exit('[' . $datetime . ']' . ' ERROR: Initialize remotedb connect error!' . PHP_EOL);
}

$start_time = $config->log_start_time;
$end_time = $config->log_end_time;

/* logpath */
$log_dir = $config->cdn_log_dir . '/';

//遍历该目录下的日志文件
$log_list = getFileList($log_dir, $start_time, $end_time);
if (!$log_list) {
	return false;
}

/* 拼接日志名称 */
/*$log_name = getLogName($start_time, $end_time);
if (!$log_name) {
	return false;
}*/

//foreach ($log_name as $key) {
foreach ($log_list as $key => $log) {

	$logAr = explode('/', $log);

	$date = substr(end($logAr), 0, 8);

	$file_dir = $config->cdn_log_dir . '/' . $date;

	//$file_path = $config->cdn_log_dir . '/' . $date . '/' . $key;
	$file_path = $log;

	$file_path_show_temp = $file_path . '.show.temp';

	$file_path_click_temp = $file_path . '.click.temp';

	//.00文件不存在或.show.temp存在.click.temp存在则跳过
	if (!file_exists($file_path) || file_exists($file_path_show_temp) || file_exists($file_path_click_temp)) {
		continue;
	}

	//匹配卡券曝光
	exec("awk '$1>='$start_time'&&$1<='$end_time'&&$4 ~ /(.jpg|.jpeg|.png)/ {print $1,$4}' '$file_path' >> '$file_path_show_temp'");

	//匹配卡券点击
	exec("awk '$1>='$start_time'&&$1<='$end_time'&&$4 ~ /url.html/ {print $1,$4}' '$file_path' >> '$file_path_click_temp'");

}

exit('[' . $datetime . ']' . ' MSG: cdn-log-cutting.php complete!' . PHP_EOL);
/**
 * 获取配置时间段的log名称
 * @param  [string] $start_time [开始时间]
 * @param  [string] $end_time [截至时间]
 * @return [array] [data array]
 */
function getLogName($start_time, $end_time) {
    global $config;
    $hours = [];
    $stime = strtotime($start_time);
    $etime = strtotime($end_time);
    while ($stime < $etime) {
        $hours[] = substr($start_time, 0, -4) . '-' . $config->cdn_host;
        $stime = $stime + 3600;
        $start_time = date('YmdHis', $stime);
    }

    return $hours;
}

/**
 * 遍历目录
 * @param  [string] $dir [文件目录]
 * @param  [string] $start_time [开始时间]
 * @param  [string] $end_time [截至时间]
 * @return [array] [data array]
 */
function getFileList($dir, $start_time, $end_time) {
    static $result = [];
    static $hours = [];
    $stime = strtotime($start_time);
    $etime = strtotime($end_time);
    while ($stime < $etime) {
        $hours[] = substr($start_time, 0, -4);
        $stime = $stime + 3600;
        $start_time = date('YmdHis', $stime);
    }

    if(!is_dir($dir)){
        exit('[' . $datetime . ']' . ' MSG: ' . $dir . '目录不存在' . PHP_EOL);
    }
    if ($dh = opendir($dir)){
        while (($file = readdir($dh)) !== false){
            if((is_dir($dir . $file)) && $file != '.' && $file != '..'){
                        getFileList($dir . $file . '/', $start_time, $end_time);
            }else{
                if($file != '.' && $file != '..'){
                    if(is_file($dir . $file) && substr($file, -2) != 'cn' && substr($file, -4) != 'temp' && in_array(substr($file, 0, 10), $hours)) {
                        $result[] = $dir . $file;
                    }
                }
            }
        }
        closedir($dh);
    }

    return $result;
}
?>